$(function() {
	var setIeBorder = function() {
		if ($.browser.msie || $.browser.opera) {
			$('table.one-flight-tbl td.v-line table td.middle').each(function() {
				$(this).height($(this).parents('td.v-line').height() - 2 * $(this).parent(0).siblings('tr').find('td').height());
			});
		}
	};

	$('a.propose_show').bind('click', function () {
		$('div.search_results').show();
		$('div.search_results.s_opened').hide();
		$('div.air-not-found').hide();
		$('div.search_results').not('.s_opened').find('a.propose_hide').hide().siblings('a.propose_show').show();
		var
			elm = this,
			$this = $(elm),
			p = $this.parents('div.search_results'),
			d = $this.metadata(),
			ajaxData = {itineraryCode: d.itineraryCode}
		;

		if (p.next(0).hasClass('s_opened')) {
			$('#popupOverlay').hide();
			$('#popupLoading').hide();
			$('#popupWindow').hide();
			p.find('a.propose_show').removeClass('popup');
			p.find('a.propose_show').removeClass('wait-screen');
			p.hide();
			p.next(0).show();
		} else {
			if ($(this).hasClass('popup')) {
				$.ajax({
					url: 'air.getExtendedData',
					data: ajaxData,
					success: function(data) {
						tm.Popup.popupClose();
						if (data == 'error') {
							errorShowFn(elm);
						} else {
							p.hide().after(data);
							p.find('a.propose_show').removeClass('popup');
							p.find('a.propose_show').removeClass('wait-screen');
							$('.service_choose :radio').each(function () {
								tm.FormControls.addRadioButtonGroup(this);
								tm.FormControls.replaceRadioButton(this);
							});
							setIeBorder();
							$('a.propose_hide').click(function() {
								var s = $(this).parents('div.search_results.s_opened');
								s.prev().show();
								s.hide();
							});
							searchResultsEvents(elm);
						}
					},
					error: function() {
						$('#popupWindow #popupCommon .c .cnt').html('<center style="font-size:24px; font-weight:bold; line-height:1em; color:#ff0000; padding:0 20px;">' + 'ERROR_ORDER_RESPONSE_TEXT'.translate() + '</center>');
						setTimeout(function() {
							tm.Popup.popupClose();
						}, 5000);
					}
				});
			} else {
				errorShowFn(elm);
			}
		}
		
	});
	
	var errorShowFn = function(elm) {
		var
			p = $(elm).parents('div.search_results'),
			errorBlock = p.find('div.air-not-found')
		;
		errorBlock.show();
		/*p.find('a.propose_show').attr('class', 'propose_show').unbind('click', tm.Popup.clickEvent).removeClass('propose_show').addClass('propose_hide').click(function() {
			errorBlock.hide();
			$(this).removeClass('propose_hide').addClass('propose_show').show().bind('click', tm.Popup.clickEvent);
		});*/
	};
	
	var nextClick = function(it, classN) {
		var
			thisTab = it.parents('div.propositions').prev('ul.air-results-tabs').find('li a.' + classN),
			nextTab = thisTab.parent(0).next(0),
			time = it.find('div.way-time .depDate').text().split(' ')[1],
			duration = it.find('div.way-time .duration').text()
		;
		thisTab.find('.time').text(time + '/' + duration);
		thisTab.parent(0).addClass('selected');
		while (nextTab.hasClass('selected')) {
			nextTab = nextTab.next(0);
		}
		nextTab.find('a').trigger('click');
		setIeBorder();
	};
	
	var searchResultsEvents = function(elm) {
		var
			form = $(elm).parents('div.search_results').next(0).find('form.frm-air-search'),
			flChoose = form.find('div.propositions .flight_choose').not('.order-flight'),
			oneRows = 0
		;
		flChoose.each(function() {
			if ($(this).find('div.row').size() == 1) {
				oneRows++;
			}
		});
		if (flChoose.size() == oneRows) {
			flChoose.find('div.row').addClass('selected');
			var flContent = flChoose.siblings('div.order-flight').find('div.flight-content');
			var resultFlights = flContent.size();
			for (var i = 1; i <= resultFlights; i++) {
				form.find('div.propositions .flight-' + i + '-content').html(form.find('div.propositions .flight-' + i + ' div.row').html());
				form.find('.flightIndex' + (i-1) + 'Main').val(0);
				form.find('.air-results-tabs a.flight-' + i).parents('li').remove();
				form.find('div.propositions .flight-' + i).remove();
			}
			flContent.find('div.choose-flight').remove();
			flContent.find('div.block-control .moreInfo').remove();
			flContent.find('div.flights-ext-info').show();
		}
		function moreInfoClick() {
			$(this).parent(0).hide();
			$(this).parents('div.row-3').find('.flights-ext-info').slideDown(function() {
				setIeBorder();
			});
		}
		form.find('.block-control a.moreInfo').unbind('mousedown').bind('mousedown', moreInfoClick);
		$('.propositions .d_propositions div.service_choose div.row').mouseover(function() {
			var $this = $(this);
			if (!$this.hasClass('selected')) {
				$this.addClass('hovered');
			}
		});
		$('div.service_choose div.row').mouseout(function() {
			$(this).removeClass('hovered');
		});
		
		function chooseFlightClick() {
			var
				$this = $(this).parents('div.row'),
				sc = $this.parents('.service_choose')
			;

			$(document.documentElement).scrollTo($this.parents('.s_opened'), {queue:true});

			sc.find('div.selected').removeClass('selected');
			$this.addClass('selected').removeClass('hovered');
			var
				html = $this.html(),
				classN = sc.attr('class').match(/flight\-./),
				content = sc.siblings('div.order-flight').find('div.' + classN + '-content')
			;
			content.html(html).find('div.choose-flight').remove();
			content.find('div.block-control .moreInfo').remove();
			content.find('div.flights-ext-info').show();
			
			var tabIndex = $this.parents('.flight_choose').attr('class');
			tabIndex = String(tabIndex.match(/flight\-\d{1}/));
			tabIndex = parseInt(tabIndex.replace('flight\-', ''));
			var rowIndex = String($this.attr('class').match(/row_num_\d{1,2}/));
			rowIndex = parseInt(rowIndex.replace('row_num_', ''));
			$(this).parents('form').find('input.flightIndex' + (tabIndex - 1) + 'Main').val(rowIndex);
			
			nextClick($this, classN);
			
		}
		
		$('div.service_choose div.row a.choose-flight').unbind('mousedown').bind('mousedown', chooseFlightClick);
		
		$('.air-results-tabs li a').unbind('click').click(function() {
			var
				$this = $(this),
				$thisLi = $this.parents('li'),
				tabNum = $this.attr('class'),
				form = $this.parents('form.frm-air-search'),
				propositions = form.find('div.propositions'),
				submit = form.find('.search_result_bottom .submit'),
				flightsSize = $this.parents('ul').find('li').size() - 1
			;
			
			if (!$thisLi.hasClass('active')) {
				$thisLi.siblings('li.active').removeClass('active');
				$thisLi.addClass('active');
				form.find('div.flight_choose').hide();
				form.find('div.' + tabNum).fadeIn();
			}
			
			if ($this.hasClass('order-flight')) {
				if (propositions.find('a.not-choosen').is(':visible')) {
					submit.hide();
				} else {
					submit.show();
				}
			} else {
				submit.hide();
			}
			setIeBorder();
			
			if ($.browser.msie) {
				if ($thisLi.hasClass('selected')) {
					propositions.find('div.row.selected a.choose-flight').unbind('mousedown').bind('mousedown', chooseFlightClick);
					propositions.find('.block-control a.moreInfo').unbind('mousedown').bind('mousedown', moreInfoClick);
				}
			}
			
		});
		
		if (flChoose.size() == oneRows) {
			form.find('.air-results-tabs a.order-flight').trigger('click').parents('li.active').css('bottom', 0);
		}
		
		$('a.not-choosen').unbind('click').click(function() {
			var $this = $(this);
			var c = $this.parents('.flight-content').attr('class');
			c = $.trim(c.replace('flight-content', '').replace('-content', ''));
			$('.air-results-tabs li a.' + c).trigger('click');
		});
	};
	
});